package net.feixiang.creational.abstractFactory;

/**
 * 抽象工厂模式示例
 *
 * @author 飞翔
 * @see <a href="https://www.feixiang.net/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E7%AC%AC%E4%B8%80%E7%AB%A0%E3%80%81%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/13.html">抽象工厂模式</a>
 */
public class AbstractFactoryDemo {
    private final GUIFactory factory;

    public AbstractFactoryDemo(GUIFactory factory) {
        this.factory = factory;
    }

    /**
     * 创建用户界面
     * 根据不同的操作系统，创建不同风格的按钮和文本框
     */
    public void createUI() {
        Button button = factory.createButton();
        TextBox textBox = factory.createTextBox();
        button.render();
        textBox.render();
    }

    public static void main(String[] args) {
        // 输出当前操作系统
        System.out.println("当前操作系统: " + System.getProperty("os.name"));

        // 根据操作系统选择工厂
        GUIFactory factory;
        if (System.getProperty("os.name").contains("Windows")) {
            factory = new WindowsFactory();
        } else {
            factory = new MacFactory();
        }
        // 如果有其它系统，直接在这里新增实现类，不需要修改 GUIFactory 和 createUI()
        // 例如：factory = new LinuxFactory();

        // 创建应用程序实例
        AbstractFactoryDemo app = new AbstractFactoryDemo(factory);
        app.createUI();
    }
}